<?php
namespace Admin\Controller;

class PublicController extends BaseController {


    public function logout() {
        session ('role',null);
        session ('admin',null);
        $this->redirect('Login/index');
    }


    /**
     * 修改个人信息公共方法
     */
    public function editPersonalInfo() {

        $roleArr = array('super','department','teacher');
        $role = session ('role');
        if (!in_array($role,$roleArr)) $this->error('非法访问');

        if (!IS_POST) {
            switch ($role) {
                case 'super':
                    $view = 'sadmin-edit';
                    break;
                case 'department':
                    $view = 'dadmin-edit';
                    $a = session('admin');
                    $Department = D ('Department');

                    $this->assign('dname',$Department->getDepartmentName($a['department_id']));
                    break;
                case 'teacher':
                    $view = 'teacher-edit';
                    break;
                default :$view = null;
            }

            if (is_null($view)) $this->error('访问错误');
            $this->display($view);
        } else {

            switch ($role) {
                case 'super':
                    $this->editSadmin('Sadmin');
                    die;
                case 'department':
                    $this->editSadmin('Dadmin');
                    die;
                case 'teacher':
                    $this->editTeacher();
                    die;
            }
        }
    }



    protected function editSadmin($model = 'Sadmin') {

        $data['id'] = $_REQUEST['id'];
        $data['email'] = $_REQUEST['email'];
        $data['telphone'] = $_REQUEST['telphone'];
        $data['tname'] = $_REQUEST['tname'];
        $data['password'] = md5($_REQUEST['password']);
        $tpassword = md5($_REQUEST['tpassword']);

        if ($data['password'] !== $tpassword)
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain('100003'));
        if ($data['password'] === md5('default')) unset($data['password']);

        $data['photo'] = I ('request.photo',null);
        if (empty($data['photo'])) unset($data['photo']);

        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        $M = D ($model);
        $flag = $M->edit($data);
        if ($flag === '000000') {
            // 重新登录
            session('role',null);
            session('admin',null);
            $this->out('RESULT_OK','修改个人资料成功,请重新登录',__APP__.'/Login');
        }
        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

    }


    protected function editTeacher() {

        $data['id'] = I ('request.id',null);
        $data['email'] = I ('request.email',null);
        $data['telphone'] = I ('request.telphone',null);
        $data['password'] = md5($_REQUEST['password']);
        $tpassword = md5($_REQUEST['tpassword']);

        if ($data['password'] !== $tpassword)
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain('100003'));
        if ($data['password'] === md5('default')) unset($data['password']);

        $data['photo'] = I ('request.photo',null);
        if (empty($data['photo'])) unset($data['photo']);

        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        $Teacher = M ('Teacher');
        $flag = $Teacher->save($data);
        if ($flag === false)
            $this->out('RESULT_FAIL','修改失败');

        // 重新登录
        session('role',null);
        session('admin',null);
        $this->out('RESULT_OK','修改个人资料成功,请重新登录',__APP__.'/Login');
    }


    // 上传头像
    public function upload() {
        $Upload = new \Api\Libs\Upload();
        $res = $Upload->upload('img');
        $this->ajaxReturn($res);
    }


}